package com.woniuxy.usercoachassemble.controller;
import com.woniuxy.usercoachassemble.entity.Attention;
import com.woniuxy.usercoachassemble.entity.Invitation;
import com.woniuxy.usercoachassemble.entity.Userinfo;
import com.woniuxy.usercoachassemble.service.AttentionService;
import com.woniuxy.usercoachassemble.service.InvitationService;
import com.woniuxy.usercoachassemble.service.UserinfoService;
import org.example.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-07-15
 */
@RestController
@RequestMapping("invitation")
public class InvitationController {
    @Autowired
    private InvitationService invitationServiceImpl;
    @Autowired
    private AdressController adressController;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private  UserinfoService userInfoServiceImpl;
    @Autowired
    private AttentionService attentionServiceImpl;
    @RequestMapping("insertinvitation")
    public  void insertinvitationbyuserid(Invitation invitation){
        invitationServiceImpl.save(invitation);
    }
//查询附近人的消息 返回消息 按照时间排序  带上昵称 不显示距离和位置
    @RequestMapping("getaroundpersoninvitation")
    public Result getaroundpersoninvitation(double distance, HttpServletRequest request){
 //获取周围人的id
         int userId=request.getIntHeader("userid");
        List <Invitation> lis=new ArrayList<>();
      //40公里以内的人 一次只返回100条 不然爆满了
        List<Integer> ids = adressController.getaroundpersonswithyou(request, distance);
        System.out.println(ids);
        for (Integer id:ids){
            //查询附近某一个人的帖子
//            查询附近人的昵称
            Userinfo userinfo=userInfoServiceImpl.query().eq("user_id",id).one();
            String nickName=userinfo.getNickname();
         List<Invitation> invitations=invitationServiceImpl.query().eq("user_id",id).list();
           for (Invitation invitation:invitations){
               invitation.setNickName(nickName);
               lis.add(invitation);
               Distance address = redisTemplate.opsForGeo().distance("address", userId + "", id + "");
               invitation.setLength(address.getValue());
           }
        }
//        按照时间排序

        lis.sort((o1, o2) -> {
         return o1.getInvitationTime().compareTo(o2.getInvitationTime());
        });
        return  Result.Success(lis);

    }

//    查询我的关注的图文信息
    //先查询我的关注的人的信息
    public  List<Attention> getallattentionpersonbyuserId(int userId){
        List<Attention> Attentions = attentionServiceImpl.query().eq("oneself_id", userId).list();
        return  Attentions;
    }

    //查询关注人留言 按照时间排序
    @RequestMapping("selectattentionpersonbyuserid")
    public Result selectattentionpersonbyuserid(HttpServletRequest request){
           int userId=request.getIntHeader("userId");
        List<Attention> Attentions=getallattentionpersonbyuserId(userId);
        List <Invitation> lis=new ArrayList<>();
        for (Attention attention:Attentions){
             //查询被关注人的帖子
            List<Invitation> invitations = invitationServiceImpl.query().eq("user_id", attention.getOthersId()).list();
     for (Invitation invitation:invitations){
         //查询被关注人信息
         Userinfo userinfo=userInfoServiceImpl.query().eq("user_id",invitation.getUserId()).one();
         String nickName=userinfo.getNickname();
         invitation.setNickName(nickName);
         invitation.setImgSrc(userinfo.getHeadPortrait());
         lis.add(invitation);
     }
            //        按照时间排序
    lis.sort((o1, o2) -> {
        return o2.getInvitationTime().compareTo(o1.getInvitationTime());
    });


        }
        return  Result.Success(lis);
    }

    //用户上传图片

}

